<template>
  <div class="app-container">
    <el-row :gutter="10">
      <!--树结构-->
      <el-col :span="isHide ? 1 : 6" :xs="24" class="relative">
        <div class="app-container-tree-border">
          <project-tree-select-inner class="cd-mb8" :projectId="projectId" @changeProject="changeProject" />
          <el-tree :data="treeList" :props="defaultProps" ref="tree" node-key="id" highlight-current
            @node-click="handleNodeClick">
            <div slot-scope="{ node, data }" class="custom-tree-node" :title="node.label" v-text="node.label"></div>
          </el-tree>
          <div class="tree-hide-button iconfont" :class="isHide ? 'icon-xiangyou' : 'icon-xiangzuo'"
            @click="() => { isHide = !isHide }">
          </div>
        </div>
      </el-col>
      <el-col :span="isHide ? 23 : 18" :xs="24">
        <div class="app-container-table-border">
          <el-table border stripe v-loading="loading" :data="dataList" :summary-method="getSummaries" show-summary
            :max-height="tableHeight">
            <el-table-column label="站点名称" align="center" prop="zdName" min-width="150px" show-overflow-tooltip>
              <template slot-scope="scope">
                <span :class="{ 'red': scope.row.total == '0.00' }">{{ scope.row.zdName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="合同金额" header-align="center" align="right" prop="contractAcount" min-width="120px" />
            <el-table-column label="变更后合同额" header-align="center" align="right" prop="actualContractAmount"
              min-width="120px" />
            <el-table-column label="开工预付款" header-align="center" align="right" prop="kgyfk" min-width="120px" />
            <el-table-column v-for="(item, index) in titleList" :key="index" :label="item.label" header-align="center"
              align="right" :prop="item.value" min-width="120px" />
            <el-table-column label="合计" header-align="center" align="right" prop="total" min-width="120px" />
          </el-table>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getPaymentList } from "@/api/cm/paymentStatistics";
import { queryBdSelect } from "@/api/base/bsBd";
import { mapGetters } from "vuex";
import decimal from "@/utils/big-decimal";

export default {
  name: "PaymentStatistics",
  components: {
  },
  data () {
    return {
      isHide: false,
      treeList: [],
      treeInfo: {},
      defaultProps: {
        children: "children",
        label: "name"
      },
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 站点表格数据
      dataList: [],
      titleList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bdId: undefined,
      },
      tableHeight: document.documentElement.scrollHeight - 130,
    };
  },
  // 监听顶部全局下拉
  computed: {
    ...mapGetters(["sysProjectId", "sysProjectList"])
  },
  watch: {
    sysProjectId (newVal, oldVal) {
      // 系统顶部选中项目变化
      this.changeProject(newVal);
    }
  },
  created () {
    this.changeProject();
  },
  methods: {
    changeProject (projectId = this.sysProjectId) {
      this.projectId = projectId;
      this.getBdList();
    },
    getBdList () {
      queryBdSelect({ projectId: this.projectId }).then((response) => {
        this.treeList = response.data;
        // 选中左侧树
        this.$nextTick(() => {
          if (this.treeList.length > 0 && this.$refs.tree) {
            this.$refs.tree.setCurrentKey(this.treeList[0].id);
            this.handleNodeClick(this.treeList[0]);
          }
        });
      });
    },
    handleNodeClick (data) {
      this.queryParams.bdId = data.id;
      this.treeInfo = data;
      this.handleQuery();
    },
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 查询站点列表 */
    getList () {
      this.loading = true;
      getPaymentList(this.queryParams).then((response) => {
        const data = response.data;
        this.titleList = data.titleList;
        this.dataList = data.list;
        this.loading = false;
      });
    },
    getSummaries (param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        if (index >= 3) {
          const values = data.map(item => Number(item[column.property]));
          if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return decimal.add(prev, curr);
              } else {
                return decimal.round(prev);
              }
            }, 0);
            sums[index] += "";
          } else {
            sums[index] = "";
          }
        }
      });

      return sums;
    },
  }
};
</script>
<style lang="scss" scoped>
.boxCenter {
  margin-top: 20px;
  text-align: center;
}

.header-title {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 10px 0;
  font-size: 20px;
  font-weight: 700;
}
</style>
